package 数组;

import java.util.HashMap;
import java.util.Map;

public class _1_两数之和 {
	
	public int[] twoSum01(int[] nums, int target) {
		for(int i = 0;i< nums.length;i++) {
			for(int j = i +1;j< nums.length;j++) {
				if(nums[i] +nums[j] == target) {
					return new int[] {i,j};
				}
			}
		}
		return new int[0];
    }
	
	public int[] twoSum(int[] nums, int target) {
		Map<Integer, Integer> map = new HashMap<>();
		for(int i=0;i<nums.length;i++) {
			int ret = target - nums[i];
			if(map.containsKey(ret)) {
				return new int[] {map.get(ret),i};
			}
			map.put(nums[i], i);
		}
		return new int[0];
	}
	
	public static void main(String[] args) {
		int[] nums = {2,7,11,15};
		_1_两数之和 test = new _1_两数之和();
		int[] ret = test.twoSum(nums, 9);
		System.out.println(ret[0]+"-"+ret[1]);
	}

}
